package com.oepm.web.controller;

import com.oepm.biz.PermissionBiz;
import com.oepm.biz.UserBiz;
import com.oepm.common.entity.Meta;
import com.oepm.common.entity.Result;
import com.oepm.common.utils.JWTUtil;
import com.oepm.dao.entity.MenusPo;
import com.oepm.dao.entity.RolePerPo;
import com.oepm.dao.entity.UserPo;
import com.oepm.common.utils.RoleUtil;
import com.oepm.web.vo.CRoleVo;
import com.oepm.web.vo.MenusVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin//解决跨域
@RestController
@Api(tags = "菜单(权限)API")
public class PermissionController {
    @Autowired
    PermissionBiz perBiz;
    @Autowired
    UserBiz userBiz;
    @GetMapping("/menus")
    @ApiOperation("获取菜单栏")
    public Result selectMenus(ServletRequest request){
        RolePerPo rolePerPo = new RolePerPo();
        //获取请求头token
        HttpServletRequest req= (HttpServletRequest) request;
        String token=req.getHeader("Authorization");
        //获取角色id
        String roleId = JWTUtil.getRoleId(token);
        System.out.println(roleId);
        rolePerPo.setRoleId(roleId);
        //查询一级菜单
        rolePerPo.setLevel("1");
        List<MenusPo> list1 = perBiz.selectMenus(rolePerPo);
        //查询二级菜单
        rolePerPo.setLevel("2");
        List<MenusPo> list2 = perBiz.selectMenus(rolePerPo);
        for(int i=0;i<list1.size();i++){
            List<MenusPo> list = new ArrayList<>();
            for(int j=0;j<list2.size();j++){
                String parId =perBiz.selectParId(list2.get(j).getId());
                if (parId.equals(list1.get(i).getId())){
                    list2.get(j).setLable(list1.get(i).getAuthName());
                    list.add(list2.get(j));
                }
            }
            list1.get(i).setChildren(list);
        }
        Result result = new Result(Meta.SUCCESS,list1);
        return result;
    }


    @PostMapping("/roleShift/menus")
    @ApiOperation("用户切换角色重新获取菜单栏")
    public Result changeMenus(@RequestBody CRoleVo cRoleVo) throws UnsupportedEncodingException {
        String userId = JWTUtil.getUserId(cRoleVo.getToken());
        //验证用户是否有该角色
        //roleid用户需要切换的角色id
        String roleId = RoleUtil.roleChange(cRoleVo.getRoleone());
        if (!userBiz.queryRoleId(userId).contains(roleId)) {
            return new Result(Meta.UNAUTHORISE);
        }
        UserPo userPo = userBiz.queryUserByUserId(userId);
        userPo.setRole(cRoleVo.getRoleone());
        if (roleId.equals("1")){
            userPo.setAcademy(null);
        }
        userPo.setPassword("");
        userPo.setRoleId(RoleUtil.roleChange(cRoleVo.getRoleone()));
        String token = JWTUtil.createToken(userPo);

        //获取菜单


        RolePerPo rolePerPo = new RolePerPo();

        rolePerPo.setRoleId(roleId);
        //查询一级菜单
        rolePerPo.setLevel("1");
        List<MenusPo> list1 = perBiz.selectMenus(rolePerPo);
        //查询二级菜单
        rolePerPo.setLevel("2");
        List<MenusPo> list2 = perBiz.selectMenus(rolePerPo);
        for(int i=0;i<list1.size();i++){
            List<MenusPo> list = new ArrayList<>();
            for(int j=0;j<list2.size();j++){
                String parId =perBiz.selectParId(list2.get(j).getId());
                if (parId.equals(list1.get(i).getId())){
                    list2.get(j).setLable(list1.get(i).getAuthName());
                    list.add(list2.get(j));
                }
            }
            list1.get(i).setChildren(list);
        }

        MenusVo menusVo = new MenusVo(list1,userPo,token);
        Result result = new Result(Meta.SUCCESS);
        result.setData(menusVo);

        return result;
    }

}
